from pub.routine import UdpClient
from rcpm.protocal import RemoteFileTrans

RCP_PORT = 31616
APP_DESC = "Remote copy by Python3, without SSL [v0.1.2] 2019/10/26"

def run_client():
    from time import sleep

    def getopt():
        import argparse
        parser = argparse.ArgumentParser("rcp", description=APP_DESC)
        parser.add_argument("ipaddr", action="store", help="远程服务端IP地址")
        parser.add_argument("action", action="store", help="选择【push】或【pull】")
        parser.add_argument("source", action="store", help="原始文件路径")
        parser.add_argument("target", action="store", help="备份文件路径")
        return parser.parse_args()

    args = getopt()
    print("IP address: ", args.ipaddr)
    client = UdpClient((args.ipaddr, RCP_PORT))
    protocal = RemoteFileTrans()
    client.set_protocal(protocal)
    client.listen()

    if args.action == "push":
        protocal.push_file(args.source, args.target)
    elif args.action == "pull":
        protocal.pull_file(args.source, args.target)

    sleep(0.1)  # 等待数据接收
    client.stop()


if __name__ == "__main__":
    run_client()
